<?php
require_once('Vega/Dao/Table.php');

class Mobitv_Admin_ScheduleCategoryDao extends Vega_Dao_Table
{

    public function getList()
    {
        $sql = "SELECT * FROM channel_schedule_category";

        $this->_db->query($sql);

        return $this->_db->fetchRowset();
    }

    public function countAll($condition) {
        if ($condition)
            $sql = "SELECT COUNT(cs.id) as numrow
                    FROM channel_schedule cs WHERE $condition";
        else
            $sql = "SELECT COUNT(id) as numrow
                    FROM channel_schedule";
        $this->_db->query($sql);
        $row = $this->_db->fetchRow();
        return $row['numrow'];
    }
    
    public function getDetail($scheduleCategoryId) {
        $scheduleCategoryId = intval($scheduleCategoryId);
        $sql = "SELECT * FROM channel_schedule_category WHERE id=$scheduleCategoryId";
        $this->_db->query($sql);
        
        return $this->_db->fetchRow();
    }

    public function save($data, $scheduleCategoryId) {
        $userId = intval($_SESSION['userid']);
        $name = $this->_db->valueQuote($data['name']);
        $code = $this->_db->valueQuote($data['code']);
        $sql = "UPDATE channel_schedule_category SET name=$name, code=$code, updated_by=$userId, updated_at=now() WHERE id=$scheduleCategoryId";
        
        return $this->_db->queryWrite($sql);
    }

    public function add($data) {
        $userId = intval($_SESSION['userid']);
        $name = $this->_db->valueQuote($data['name']);
        $code = $this->_db->valueQuote($data['code']);
        
        $sql = "INSERT INTO channel_schedule_category SET name=$name, code=$code, created_by=$userId, created_at=now(), updated_by=$userId, updated_at=now()";
        return $this->_db->queryWrite($sql);
    }

    public function deleteFromList($list) {
        $list = "(" . implode(",", $list) . ")";
        $sql = "DELETE FROM channel_schedule_category WHERE id IN $list";
        
        return $this->_db->queryWrite($sql);
    }

    /*
     * Danh dau chuong trinh la hot
     */
    public function makeHot($list)
    {
        $list = "(" . implode(",", $list) . ")";
        $sql = "UPDATE channel_schedule SET is_hot=1 WHERE id IN $list";
        return $this->_db->queryWrite($sql);
    }
}